package com.lg.system.role.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lg.shiro.UserRealm;
import com.lg.system.menu.entity.Menu;
import com.lg.system.menu.service.MenuService;
import com.lg.system.role.dao.RoleMenuDao;
import com.lg.system.role.entity.RoleMenu;
import com.lg.system.role.service.RoleMenuService;
import com.lg.utils.BuildTree;
import com.lg.utils.Tree;

/**
 * <p>
 * 角色与菜单对应关系 服务实现类
 * </p>
 *
 * @author dl
 * @since 2018-03-23
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuDao, RoleMenu> implements RoleMenuService {

	@Autowired
	MenuService menuService;
	
	@Override
	public Tree<Menu> selectMenuByRole(Long roleId,Long userId) {
		//给菜单分配权限时，首先查询该用户是否是超级管理员（id=1） ,超级管理员查询全部菜单，非超级管理员值查询自己拥有的权限
		Map<String, Object> param=new HashMap<>();
		int k=0;
		List<Menu> menus=new ArrayList<>();
		if(userId.equals(1L)){//查询所有的菜单
			menus=menuService.selectByMap(param);
		}else{
			menus=menuService.selectMenuUid(userId);
		}
		param.put("role_id", roleId);
		List<Long> menuIds=selectMenus(param);//查询当前角色所拥有的菜单用于编辑时还原勾选状态
		List<Tree<Menu>> trees = new ArrayList<Tree<Menu>>();
		for (Menu menu : menus) {
			Tree<Menu> tree = new Tree<Menu>();
			tree.setId(menu.getMenuId().toString());
			tree.setParentId(menu.getParentId().toString());
			tree.setName(menu.getName());
			if(menuIds.contains(menu.getMenuId())){
				tree.setChecked(true);
				k+=1;
			}
			trees.add(tree);
		}
		List<Tree<Menu>> topNodes= BuildTree.buildList(trees,"0");
		Tree<Menu> root = new Tree<Menu>();
		if (topNodes.size() == 1) {
			root = topNodes.get(0);
		} else {
			root.setId("-1");
			root.setParentId("0");
			root.setHasParent(false);
			root.setChildren(true);
			root.setChildren(topNodes);
			root.setName("顶级节点");
			if(k!=0)
			root.setChecked(true);
		}
		return root;
	}
	
	private List<Long> selectMenus(Map<String, Object> param){
		List<Long> menuIds=new ArrayList<>();
		List<RoleMenu> menus=selectByMap(param);
		if(!menus.isEmpty()){
			for(RoleMenu menu:menus){
				menuIds.add(menu.getMenuId());
			}
		}
		return menuIds;
	}

	@Override
	public boolean assignMenu(Long roleId, List<Long> menuIds) {
		boolean f=deleteByRoleId(roleId);
		if(f){
			if(!menuIds.isEmpty()&&menuIds.size()==1&&menuIds.get(0).intValue()==-1){//全部删除
				return true;
			}else{
				List<RoleMenu> roleMenus=new ArrayList<>();
				for(Long menuId:menuIds){
					if(menuId.intValue()==-1)
					continue;
					RoleMenu roleMenu=new RoleMenu();
					roleMenu.setRoleId(roleId);
					roleMenu.setMenuId(menuId);
					roleMenus.add(roleMenu);
				}
				return insertBatch(roleMenus);
			}
		}else{
			return false;
		}
	}
	
	@Override
	public boolean deleteByRoleId(Long roleId) {
		Map<String, Object> param=new HashMap<>(16);
		param.put("role_id", roleId);
		return deleteByMap(param);
	}

	@Override
	public boolean deleteByMenuId(Long menuId) {
		Map<String, Object> param=new HashMap<>(16);
		param.put("menu_id", menuId);
		return deleteByMap(param);
	}

}
